package com.lmk.system.service.Impl;

import com.lmk.basic.service.Impl.BaseServiceImpl;
import com.lmk.system.domain.Menu;
import com.lmk.system.mapper.MenuMapper;
import com.lmk.system.service.IMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class MenuServiceImpl extends BaseServiceImpl<Menu> implements IMenuService {
    @Autowired
    private MenuMapper mapper;

    // 获取菜单树
    @Override
    public List<Menu> menuTree() {
        // 查询所有Department对象
        List<Menu> list = mapper.loadAll();
        // 定义一个map集合
        HashMap<Long, Menu> map = new HashMap<>();
        for (Menu menu : list) {
            map.put(menu.getId(),menu);
        }

        List<Menu> menuTree = new ArrayList<>();
        for (Menu menu : list) {
            if(menu.getParentId() == null) {//顶级部门
                menuTree.add(menu);
            }else{
                Long parent_id = menu.getParentId();
                Menu menu1 = map.get(parent_id);
                menu1.getChildren().add(menu);
            }
        }
        return menuTree;
    }
}
